// map.js

import qqMap from "../../../utils/qqMap"

Page({
  data: {
    latitude: 36.08822,
    longitude: 111.51962,
    markers:[{
      id:1,
      latitude: 36.08822,
      longitude: 111.51962,
      iconPath:"/static/images/weizhi.png",
      width:40,
      height:40
    },{
      id:2,
      latitude: 36.08162478773236,
      longitude: 111.52704316937258,
      iconPath:"/static/images/marker.png",
      width:40,
      height:40
    }],
    repicDetail:{},
    polyline:{}
  },
  onLoad(ev){
    // 获取详情信息
    this.getRepicDetail(ev.id)
    this.getMapDetail()
    this.repicid = ev.id
  },
  // 获取详情信息
  async getRepicDetail(id){
    const {code,data} = await wx.myRequest.get(`/repair/${id}`)
    if(code !== 10000) return wx.utils.toast("获取详情信息失败")
    this.setData({
      repicDetail:data
    })

  },
  // 获取地图信息
  getMapDetail(){
    qqMap.direction({
      mode:"bicycling",
      from:"36.08162478773236,111.52704316937258",
      to:"36.08822,111.52704316937258",
      success:(res,data) => {
        if (res.message === "Success"){
          const coors = data[0].polyline
          const pl = []
          // const kr = 1000000;
          //坐标解压（返回的点串坐标，通过前向差分进行压缩）
          for (var i = 2; i < coors.length ; i++){
            coors[i] = coors[i-2] + coors[i]/1000000
          }
           //将解压后的坐标放入点串数组pl中
          for (var i = 0; i < coors.length; i += 2) {
            pl.push({ latitude: coors[i], longitude: coors[i + 1] })
          }
          this.setData({
            latitude: 36.08822,
            longitude: 111.51962,
            polyline:[
              {
                points:pl,
                color:'#5591af',
                width:4,
              }
            ]
          })
        } 
      },
      fail:(ev) =>{
        console.log(ev);
      }
    })
  },
  // 修改报修信息
  updateRepicInfo(ev){
    console.log(ev.mark.id);
    wx.navigateTo({
      url:`/repair_pkg/pages/form/index?id=${ev.mark.id}`
    })
  },
  // 取消报修
  async cecalRepic(ev){
    switch (this.data.repicDetail.status) {
      case 2:
        wx.utils.toast("当前状态不允许取消")
        break;
      case 1:
        const {code} = await wx.myRequest.get("/help/updateStatus",{
          type:"repair",
          id:ev.mark.id,
          status:0
        })
        if(code !== 10000) return wx.utils.toast("请求失败！")
        this.data.repicDetail.status = 0
        // this.setData({
        //   repicDetail["status"]:this.data.repicDetail.status
        // })
        wx.navigateTo({
          url: '/repair_pkg/pages/list/index',
        })
        break;
      default:
        wx.utils.toast("当前状态不允许取消")
        break;
    }
  }
  
})
